using System;
using System.Reflection;


namespace Open.Data.Persistence
{
	public abstract class Table: IPersistable, IConcurrency
	{
		//Property variables
		private Concurrency _concurrency;
        private bool _useConcurrency;

		//Constructors
		public Table()
		{
            Concurrency = false;
		}

        public Table(bool concurrency)
        {
            Concurrency = concurrency;
        }

        //Properties
        public virtual bool Concurrency
        {
            get
            {
                return _useConcurrency;
            }
            set
            {
                _useConcurrency = value;
            }
        }

		//Methods
		public virtual void Update(Command command)
		{
            if (command == null) throw new ArgumentNullException("command");
    		command.Update(this);
		}

        public virtual void Select(Command command)
		{
            if (command == null) throw new ArgumentNullException("command");
            if (Concurrency && GetConcurrency() == null) SetConcurrency(command.Context.CreateConcurrency());
            command.Select(this);
		}

        public virtual void Insert(Command command)
		{
            if (command == null) throw new ArgumentNullException("command");
            command.Insert(this);
        }

        public virtual void InsertPartial(Command command)
		{
            if (command == null) throw new ArgumentNullException("command");
            command.InsertPartial(this);
        }

        public virtual void Delete(Command command)
		{
            if (command == null) throw new ArgumentNullException("command");
            command.Delete(this);
        }

        public virtual void DeletePartial(Command command)
		{
            if (command == null) throw new ArgumentNullException("command");
            command.DeletePartial(this);
		}

        public virtual bool Exists(Command command)
		{
            if (command == null) throw new ArgumentNullException("command");
			return command.Exists(this);
		}

        public virtual Concurrency GetConcurrency()
        {
            return _concurrency;
        }

        public virtual void SetConcurrency(Concurrency value)
        {
            _concurrency = value;
        }
	}
}
